﻿
    var provSel;
    var citySel;
    var distSel;
    $(function () {
     
         provSel = document.getElementById("_fm_d_0_pr");
         citySel = document.getElementById("_fm_d_0_c");
         distSel = document.getElementById("_fm_d_0_a");
        var cv = DistrictSelector.attach(provSel, citySel, distSel);
        cv.initSelectedCodes($("#divisionCode").val());
        var validate = function () {
            if (provSel.options.length > 1 && provSel.selectedIndex == 0)
                return false;
            if (citySel.options.length > 1 && citySel.selectedIndex == 0)
                return false;
            if (distSel.options.length > 1 && distSel.selectedIndex == 0)
                return false;
            return true;
        }

        $.post("/User/MyAddress", Address, "json");
        window['distSelector'] = cv;
    }

);
    var selectedGA = false;
    function doChange(selObj) {
        var selValue = selObj.options[selObj.selectedIndex].value;
        var zip = document.getElementById("_fm_d_0_po");
        if (selValue == "810000") {
            zip.value = "999077";
            selectedGA = true;
        } else if (selValue == "820000") {
            zip.value = "999078";
            selectedGA = true;
        } else if (selectedGA) {
            zip.value = "";
            selectedGA = false;
        } else {
            selectedGA = false;
        }
    }
   
    var validate = function () {
        if (provSel.options.length > 1 && provSel.selectedIndex == 0)
            return false;
        if (citySel.options.length > 1 && citySel.selectedIndex == 0)
            return false;
        if (distSel.options.length > 1 && distSel.selectedIndex == 0)
            return false;
        return true;
    }

    function Address(data) {
        var dataList = data.list;
        $(".tbl-main tbody tr").slice(1).remove();
        var tr = "";
        if (dataList != null && dataList.length > 0) {

            for (var i = 0; i < dataList.length; i++) {
               
            var obj=dataList[i];
            var id=obj.AID;
            var Consignee=obj.AConsignee;
            var Address=obj.Address;
            var AreaCode=obj.AreaCode;
            var Phone=obj.APhone;
            var Tel=obj.ATel;
            var ZipCode = obj.AZipCode;
            var mobie = "";
            var def = obj.AISDefalut ? "1" : "0";

            deliverAddr[id] = [Consignee, AreaCode, Address, ZipCode, Tel, Phone, def];

            if (obj.AISDefalut)
            {
            tr += '<tr class="thead-tbl-address" id="'+id+'" bgcolor="#C6D9F0">';
            }else{
            tr += '<tr class="thead-tbl-address" id="'+id+'" >';
        }

      
                tr += ' <td class="cell-align-center">'+Consignee+' </td>';
                tr += '<td>' + distSelector.getText(AreaCode) + '</td>';
                tr += '<td>' + Address + '</td>';
                if (Tel != "") {
                    mobie = Tel;
                }
                mobie = mobie == "" ? Phone : mobie + "/" + Phone;
                tr += ' <td class="cell-align-center">'+ZipCode+'</td>';
                tr += '<td>' + mobie + '</td>';

                if (obj.AISDefalut) {
                    tr += '<td class="thead-tbl-status" style="color: blue">默认地址</td>';
                }
                else {
                    tr += '<td class="thead-tbl-status" ><a href="#;" onclick="Updatestatus(' + id + ')">设为默认</a></td>';
                }
                tr += '<td class="cell-align-center">';
                tr += '<a href="#;" onclick="selectDeliver('+id+')">修改</a> | <a href="#;" onclick="del('+id+')">删除</a>';
                tr += ' </td>';
                tr += '</tr>';
            }
        }
        $(".tbl-main tbody").append(tr);
    }
    function quanjiao2Banjiao(str) {
        if (str == "" || typeof (str)=="undefined") {
            return "";
        }
        var i;
        var result = '';
        
        for (i = 0; i < str.length; i++) {
            code = str.charCodeAt(i);
            if (code >= 65281 && code < 65373) {
                result += String.fromCharCode(str.charCodeAt(i) - 65248);
            }
            else {
                result += str.charAt(i);
            }
        }
        return result;
    }
    function createOrUpdate() {

        $("#mobilePhone").val(quanjiao2Banjiao($("#mobilePhone").val()));
        $("#_fm_d_0_po").val(quanjiao2Banjiao($("#_fm_d_0_po").val()));

        if ($("#_fm_d_0_f").val() == '') {
            alert('\u6536\u8D27\u4EBA\u59D3\u540D\u4E0D\u80FD\u4E3A\u7A7A');
            return false;
        }

        if (!validate()) {
            //地址必填弹出框
            alert('\u8bf7\u9009\u62e9\u6709\u6548\u7684\u7701\u5e02\u533a');
            return false;
        }
       
        var address = $("#_fm_d_0_ad").val();
        if (address == ''|| typeof (address) == "undefined")
        {
            alert('\u8857\u9053\u5730\u5740\u4E0D\u80FD\u4E3A\u7A7A');
            return false;
        }
        if (address != '' && /^\d*$/.test(address)) {
            alert('\u8857\u9053\u5730\u5740\u4e0d\u80fd\u5168\u662f\u6570\u5b57');
            return false;
        }
        var codes = distSelector.getSelectedCodes();
        $("#divisionCode").val(codes[2]);
        
        PhoneCheck.easyInit();
        var isValidPhone = PhoneCheck.attachPhoneCheck(); //电话必填弹出框

        var phone = $("#phoneCode").val().trim();
        var mobilPhone = $("#mobilePhone").val().trim();
        var phonearea = $("#phoneSection").val().trim();
        isMobilPhone = /^1[3,4,5,8]\d{9}$/.test(mobilPhone);
        if (!isValidPhone) {
           
            return false;
        }
        if (mobilPhone && !isMobilPhone) {
            alert("手机格式不正确");
            return false;
        }

       
            $("#hfstatusAddr").val($("#statusAddr").attr("checked")?"1":"0");
       
       
      
        $.post("/User/SaveAddress", $("#myAddress").serialize(), function (data) {

            if (data.ResultCode != "00") {
                alert(data.ResultMsg);
            } else {
                Address(data);
                
            }
        }, "json");
       document.forms["myAddress"].reset();
    }
    var deliverAddr = [];
    
    function selectDeliver(obj) {
        $("#main-content .entity-h3").html("修改收货地址");

        var da = deliverAddr[obj];
        if (da) {
            var fullName = da[0];
            var divisionCode = da[1];
            var address = da[2];
            var post = da[3];
            var phone = da[4];
            var mobile = da[5];
            var status = da[6];
            document.getElementById('id').value = obj;
            document.getElementById('_fm_d_0_f').value = fullName;
            document.getElementById('_fm_d_0_ad').value = address;
            document.getElementById('_fm_d_0_po').value = post;
            
            document.getElementById('mobilePhone').value = mobile;
            document.getElementById('formatPhone').value = phone;
            document.getElementById('statusAddr').checked = status == 1 ? true : false;

            try {
            distSelector.initSelectedCodes(divisionCode);
            } catch (e) { }
                        try {
                            PhoneCheck.initFixedPhone($('#formatPhone'));
                        } catch (e) { }
            
        }
    }
    
    function del(obj) {
        $.post("/User/DelAddress", { ID: obj }, function (data) {

            if (data.ResultCode != "00") {
                alert(data.ResultMsg);
            } else {
                Address(data);
            }
        }, "json");
    }
    function Updatestatus(obj) {
        $.post("/User/UpdateAddress", { ID: obj }, function (data) {

            if (data.ResultCode != "00") {
                alert(data.ResultMsg);
            } else {
                Address(data);
            }
        }, "json");
    }
